package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import dao.DishDao;
import po.Dish;
import util.DBUtil;

public class DishDaoImpl implements DishDao{
	@Override
	public Map<Integer, Dish> getAllDish() {
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		Dish dish= null;
		Map<Integer,Dish> dishMap = new HashMap<Integer,Dish>();
		try {
			con = DBUtil.getConnection();
			String sql = "select * from DISH";
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				Integer did = rs.getInt("id");
				dish = new Dish();
				dish.setId(did);
				dish.setName(rs.getString("name"));
				dish.setPrice(rs.getDouble("price"));
				dish.setImg(rs.getString("img"));
				dishMap.put(did, dish);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, rs);
		}
		return dishMap;
	}

}
